package com.mdd.admin.controller.order;

import com.alipay.api.AlipayApiException;
import com.mdd.admin.service.order.IOrderInfoService;
import com.mdd.admin.validate.order.*;
import com.mdd.admin.vo.order.OrderDetailVo;
import com.mdd.common.aop.CurrentUser;
import com.mdd.common.aop.Log;
import com.mdd.common.aop.NotPower;
import com.mdd.common.base.page.PageValidate;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.order.OrderList;
import com.mdd.common.validate.IdValidate;
import com.mdd.common.validator.annotation.IDMust;
import com.mdd.system.vo.SystemAuthAdminInformVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

@RestController
@RequestMapping("api/order")
@Api(tags = "租车订单管理")
public class OrderInfoController {

    @Resource
    IOrderInfoService iOrderInfoService;

    @NotPower
    @GetMapping("/list")
    @ApiOperation(value="订单列表")
    public AjaxResult<PageResult<OrderList>> list(@Validated PageValidate pageValidate,
                                                  @Validated OrderInfoSearchValidate searchValidate,
                                                  @CurrentUser SystemAuthAdminInformVo systemAuthAdmin) {
        searchValidate.setOrderType(0L);
        PageResult<OrderList> list = iOrderInfoService.list(pageValidate, searchValidate,systemAuthAdmin);
        return AjaxResult.success(list);
    }
    @NotPower
    @GetMapping("/detail")
    @ApiOperation(value="订单详情")
    public AjaxResult<OrderDetailVo> detail(@Validated @IDMust() @RequestParam("orderId") Long orderId) {
        OrderDetailVo detail = iOrderInfoService.detail(orderId);
        return AjaxResult.success(detail);
    }
    @NotPower
    @Log(title = "订单新增")
    @PostMapping("/add")
    @ApiOperation(value="订单新增")
    public AjaxResult<Object> add(@Validated @RequestBody OrderInfoCreateValidate createValidate) {
        iOrderInfoService.add(createValidate);
        return AjaxResult.success();
    }

    @NotPower
    @Log(title = "添加备注")
    @PostMapping("/remark")
    @ApiOperation(value="订单编辑")
    public AjaxResult<Object> addRemark(@RequestParam("orderId") String orderId,@RequestParam("remark") String remark) {
        iOrderInfoService.addRemark(orderId,remark);
        return AjaxResult.success();
    }
    @NotPower
    @Log(title = "确认订单")
    @PostMapping("/confirm")
    @ApiOperation(value="订单编辑")
    public AjaxResult<Object> confirm(@RequestParam("orderId") String orderId) {
        iOrderInfoService.confirm(orderId);
        return AjaxResult.success();
    }
    @NotPower
    @Log(title = "完成订单")
    @GetMapping("/finish")
    @ApiOperation(value="完成订单")
    public AjaxResult<Object> finish(@RequestParam("orderId") Long orderId) {
        try {
            iOrderInfoService.finish(orderId);
            return AjaxResult.success();
        } catch (AlipayApiException e) {
            return AjaxResult.failed(e.getErrMsg());
        }
    }
    @NotPower
    @Log(title = "订单编辑")
    @PostMapping("/edit")
    @ApiOperation(value="订单编辑")
    public AjaxResult<Object> edit(@Validated @RequestBody OrderInfoUpdateValidate updateValidate) {
        iOrderInfoService.edit(updateValidate);
        return AjaxResult.success();
    }
    @NotPower
    @Log(title = "订单取消")
    @GetMapping("/cancel")
    @ApiOperation(value="订单取消")
    public AjaxResult<Object> cancel(@RequestParam("orderId") Long orderId) {
        try {
            iOrderInfoService.cancel(orderId);
            return AjaxResult.success();
        } catch (AlipayApiException e) {
            return AjaxResult.failed(e.getErrMsg());
        }
    }
    @NotPower
    @Log(title = "订单绑定车辆")
    @PostMapping("/change")
    @ApiOperation(value="订单绑定车辆")
    public AjaxResult<Object> changeCar(@Validated @RequestBody OrderBindCarValidate orderBindCarValidate) {
        iOrderInfoService.changeCar(orderBindCarValidate);
        return AjaxResult.success();
    }
    //订单状态：0待支付 1待签约 2车辆准备中3待提车4租赁中5待结算6待确认7已完成8已取消
    @NotPower
    @Log(title = "交车")
    @PostMapping("/deliverVehicle")
    @ApiOperation(value="交车")
    public AjaxResult<Object> deliverVehicle(@Validated @RequestBody OrderDeliverVehicleValidate orderDeliverVehicleValidate) {
        iOrderInfoService.deliverVehicle(orderDeliverVehicleValidate);
        return AjaxResult.success();
    }
    @NotPower
    @Log(title = "还车")
    @PostMapping("/returnVehicle")
    @ApiOperation(value="交车")
    public AjaxResult<Object> returnVehicle(@Validated @RequestBody OrderDeliverVehicleValidate orderDeliverVehicleValidate) {
        iOrderInfoService.returnVehicle(orderDeliverVehicleValidate);
        return AjaxResult.success();
    }

    @Log(title = "订单删除")
    @PostMapping("/del")
    @ApiOperation(value="订单删除")
    public AjaxResult<Object> del(@Validated @RequestBody IdValidate idValidate) {
        iOrderInfoService.del(idValidate.getId());
        return AjaxResult.success();
    }

}
